package com.ruoyi.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.common.Result;
import com.ruoyi.business.entity.Orders;
import com.ruoyi.business.entity.RoomInfo;
import com.ruoyi.business.entity.dto.RoomInfoDTO;
import com.ruoyi.business.service.FrontOrdersService;
import com.ruoyi.business.service.FrontRoomInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ao
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/business/roomInfo")
@Api(value = "房间信息接口",tags = "房间信息接口")
public class FrontRoomInfoController {


    @Autowired
    private FrontRoomInfoService frontRoomInfoService;

    @Autowired
    private FrontOrdersService frontOrdersService;


    @GetMapping("/list")
    @ApiOperation(value = "房间列表(特定民宿下的房间列表)")
    public Result<List<RoomInfo>> getList(@Param("homestayid") Long homestayid){
        if(homestayid == null) {
            return Result.error("数据接收错误");
        }
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getHomestayId,homestayid);
        List<RoomInfo> list = frontRoomInfoService.list(lambdaQueryWrapper);
        return Result.success(list);

    }

    /**
    * 房间预订信息查询
    *
     * */
    @ApiOperation(value = "房间列表展示",tags = "房间列表展示")
    @PostMapping("/roomstatuslist")
    public Result<List<RoomInfo> > getinfo(@RequestBody RoomInfoDTO roomInfoDTO){
        if(roomInfoDTO==null){
            return Result.error("参数传输错误!");
        }
        //查询该民宿下的所有房间信息
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getHomestayId,roomInfoDTO.getHomestayid());
        List<RoomInfo> alllist = frontRoomInfoService.list(lambdaQueryWrapper);


        //查询该民宿下所有被预订且没有完成的房间订单
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getHomestayid,roomInfoDTO.getHomestayid())  //当前民宿
                    .eq(Orders::getGoodsType,0)  //房间类型
                .eq(Orders::getOrderType,1);  //处于预订状态
        List<Orders> Orderlist = frontOrdersService.list(queryWrapper);


        for(RoomInfo room:alllist){
            boolean isflage   = true;  //标志第二层循环是否遍历完
            for(Orders order:Orderlist){
                //计算订单结束时间
                LocalDateTime endDateTime = order.getStartTime().plusDays(order.getGoodsNum());

              //判断在当前选择时间和订单时间是否冲突

                //判断开始时间是否在订单预订期间
                boolean startafter = roomInfoDTO.getStarttime().isAfter(order.getStartTime());
                boolean startbefore = roomInfoDTO.getStarttime().isBefore(endDateTime);

                //判断结束时间是否在预订期间
                boolean endafter = roomInfoDTO.getEndtime().isAfter(order.getStartTime());
                boolean endbefore = roomInfoDTO.getEndtime().isBefore(endDateTime);

                //开始或者结束时间在预订期间都不能满足情况
                if((startbefore && startafter) || (endafter && endbefore)){
                    //将房间状态修改为被预订
                    room.setIsReserve(1);
                    isflage = false;
                    break;
                }


            }
            if(false){  //说明能够被预订，将房间预订状态修改为0表示可预订
                room.setIsReserve(0);
            }
        }


        return Result.success(alllist);



    }












}

